package com.jeecms.edu.servlet;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONObject;
import com.jeecms.common.web.ResponseUtils;
import com.jeecms.edu.entity.SchoolInformation;

/**
 *  list页面 检索条件
 */
@WebServlet("/CommonSearch")
public class CommonSearch extends HttpServlet {
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		String area=request.getParameter("area");
		String level=request.getParameter("level");
		String time=request.getParameter("time");
		String nature=request.getParameter("nature");
		String cost=request.getParameter("cost");
		String action=request.getParameter("action");
		String others1=request.getParameter("others1");
		String others2=request.getParameter("others2");
		String others3=request.getParameter("others3");
		String result="";	//查询结果集
		String sql="";		//条件查询语句
		JSONObject jsonObject=new JSONObject();
		if (null!=area || null!=level || null!=time || null!=nature || null!=cost || null!=action ||null!=others1 ||null!=others2 ||null!=others3) {
			if (null!=area) {
				sql=" and region='"+area+"'";
			}if(null!=level){
				switch (level) {
				case "幼儿园":
					level="('幼儿园')";
					break;
				case "小学":
					level="('9年制学校','12年制学校','小学')";
					break;
				case "初中":
					level="('9年制学校','4年制初级中学','12年制学校','7年制完全中学')";
					break;
				case "高中":
					level="('12年制学校','3年制高级中学','7年制完全中学')";
					break;
				}
				sql=sql+" and schoollength in"+level;
			}if(null!=time){
				switch (time) {
				case "幼儿园(托儿所)":
					time="幼儿园";
					break;
				case "5年小学":
					time="小学";
					break;
				case "4年初级中学":
					time="4年制初级中学";
					break;
				case "9年制学校":
					time="9年制学校";
					break;
				case "7年制完中":
					time="7年制完全中学";
					break;
				case "12年制学校":
					time="12年制学校";
					break;
				}
				sql=sql+" and schoollength='"+time+"'";
			}if(null!=nature){
				switch (nature) {
				case "公办学校":
					nature="公办";
					break;
				case "民办学校":
					nature="民办";
					break;
				}
				sql=sql+" and schoolnature='"+nature+"'";
			}if(null!=cost){
				switch (cost) {
				case "免费":
					cost="=0";
					break;
				case "1万以下":
					cost=" between 1 and 10000";
					break;
				case "1万-2万":
					cost=" between 10000 and 20000";
					break;
				case "2万-5万":
					cost=" between 20000 and 50000";
					break;
				case "5万-10万":
					cost=" between 50000 and 100000";
					break;
				case "10-20万":
					cost=" between 100000 and 200000";
					break;
				case "20万以上":
					cost=">=200000";
					break;
				}
				sql=sql+" and tuition"+cost;
			}if(null!=action){
				sql=sql+" and enrollment='"+action+"'";
			}if(null!=others1){
				sql=sql+" and otherschoolbus=1";
			}if(null!=others2){
				sql=sql+" and otherschoolbed in(1,2)";
			}if(null!=others3){
				sql=sql+" and otherschoolkey=1";
			}
		}else{
			sql="";
		}
		List list=SchoolInformation.getPageSchool(sql);
		int size=list.size();
		request.getSession().getServletContext().setAttribute("schoollist", list);
		if (size>3) {   //增加显示数量限制
			size=3;
		}
		for (int i = 0; i < size; i++) {
			Map map=(Map) list.get(i);
			String school=(String) map.get("schoolname");
			String changes="javascript:gotoDetail('"+school+"');";
			String schoolbus=(String)map.get("otherschoolbus");
			String schoolbed=(String)map.get("otherschoolbed");
			String schoolkey=(String)map.get("otherschoolkey");
			String special="";
			if (schoolbus.equals("1")) {
				schoolbus="<li class='f01'>有校车</li>";
			}else{
				schoolbus="";
			}
			if (schoolbed.equals("1") || schoolbed.equals("2")) {
				schoolbed="<li class='f02'>有住宿</li>";
			}else{
				schoolbed="";
			}
			if (schoolkey.equals("1")) {
				schoolkey="<li class='f04'>重点学校</li>";
			}else{
				schoolkey="";
			}
			if (schoolbus=="" && schoolbed=="" && schoolkey=="") {
				special="";
			}else{
				special="<h6>学校特色</h6><ul>"+schoolbus+schoolbed+schoolkey+"</ul>";
			}
			result+="<div class='school_thread'><a href="+changes+"><img src='"+map.get("schoolimgurl")+"' class='school_picture' /></a>"
					+"<h2><a href="+changes+">"+map.get("schoolname")+"</a></h2>"
					+"<h4><span class='blue'>"+map.get("schoolregion")+"</span> 地址："+map.get("schooladdress")+" 电话："+map.get("schooltel")+"</h4>"
					+"<h5><img src='../images/icon_12.gif' />"+map.get("schoolnature")+" "+map.get("schoollength")+" "+map.get("schoolgroup")+"</h5>"
					+special
					+"<h3>学校简介</h3><p>"+map.get("schoolprofile")+"</p></div>";
		}
		jsonObject.put("total", list.size());
		jsonObject.put("result", result);
		String json = jsonObject.toJSONString();
		ResponseUtils.renderJson(response, json);
	}

}
